<template>
  <div class="register-container">
    <div class="register-card">
      <h2>用户注册</h2>
      <form @submit.prevent="handleRegister">
        <div class="form-group">
          <label for="username">用户名</label>
          <input
            type="text"
            id="username"
            v-model="form.username"
            placeholder="请输入用户名"
            required
            minlength="3"
            maxlength="20"
          />
        </div>
        <div class="form-group">
          <label for="password">密码</label>
          <input
            type="password"
            id="password"
            v-model="form.password"
            placeholder="请输入密码"
            required
            minlength="6"
          />
        </div>
        <div class="form-group">
          <label for="confirmPassword">确认密码</label>
          <input
            type="password"
            id="confirmPassword"
            v-model="form.confirmPassword"
            placeholder="请再次输入密码"
            required
          />
        </div>
        <button type="submit" class="register-button">注册</button>
        <div class="login-link">
          已有账号？<router-link to="/login">立即登录</router-link>
        </div>
      </form>
      <div v-if="errorMessage" class="error-message">{{ errorMessage }}</div>
      <div v-if="successMessage" class="success-message">{{ successMessage }}</div>
    </div>
  </div>
</template>

<script>
import { ref } from 'vue'
import { useRouter } from 'vue-router'

export default {
  name: 'RegisterView',
  setup() {
    const router = useRouter()
    const form = ref({
      username: '',
      password: '',
      confirmPassword: ''
    })
    const errorMessage = ref('')
    const successMessage = ref('')

    const handleRegister = () => {
      // 表单验证
      if (form.value.password !== form.value.confirmPassword) {
        errorMessage.value = '两次输入的密码不一致'
        setTimeout(() => {
          errorMessage.value = ''
        }, 3000)
        return
      }

      // 检查用户名是否已存在
      const users = JSON.parse(localStorage.getItem('users') || '[]')
      const existingUser = users.find(u => u.username === form.value.username)

      if (existingUser) {
        errorMessage.value = '用户名已存在'
        setTimeout(() => {
          errorMessage.value = ''
        }, 3000)
        return
      }

      // 注册成功，保存用户信息到本地存储
      const newUser = {
        id: Date.now().toString(),
        username: form.value.username,
        password: form.value.password,
        createdAt: new Date().toISOString(),
        items: [] // 用户发布的商品列表
      }

      users.push(newUser)
      localStorage.setItem('users', JSON.stringify(users))

      // 显示成功信息
      successMessage.value = '注册成功！即将跳转到登录页面...'
      errorMessage.value = ''

      // 清空表单
      form.value = {
        username: '',
        password: '',
        confirmPassword: ''
      }

      // 3秒后跳转到登录页面
      setTimeout(() => {
        router.push('/login')
      }, 3000)
    }

    return {
      form,
      errorMessage,
      successMessage,
      handleRegister
    }
  }
}
</script>

<style scoped>
/* 注册页面容器 */
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  padding: var(--spacing-lg);
  background: linear-gradient(135deg, var(--color-primary-light), var(--color-primary-dark));
  position: relative;
  overflow: hidden;
}

/* 背景装饰 */
.register-container::before {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: radial-gradient(circle, rgba(255, 255, 255, 0.1) 10%, transparent 10.5%);
  background-size: 20px 20px;
  animation: moveBackground 30s linear infinite;
}

@keyframes moveBackground {
  0% {
    transform: translate(0, 0);
  }
  100% {
    transform: translate(50px, 50px);
  }
}

/* 注册卡片 */
.register-card {
  background: white;
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-xl);
  padding: var(--spacing-xxl);
  width: 100%;
  max-width: 420px;
  z-index: 1;
  animation: fadeIn 0.5s ease-out;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 标题样式 */
.register-card h2 {
  margin-bottom: var(--spacing-xl);
  text-align: center;
  color: var(--color-heading);
  font-size: 2rem;
  font-weight: 700;
  position: relative;
}

.register-card h2::after {
  content: '';
  position: absolute;
  bottom: -10px;
  left: 50%;
  transform: translateX(-50%);
  width: 50px;
  height: 3px;
  background: linear-gradient(90deg, var(--color-primary-light), var(--color-primary));
  border-radius: var(--radius-full);
}

/* 表单组样式 */
.form-group {
  margin-bottom: var(--spacing-lg);
  position: relative;
}

.form-group label {
  display: block;
  margin-bottom: var(--spacing-sm);
  font-weight: 600;
  color: var(--color-text-secondary);
  font-size: 0.9rem;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

/* 输入框样式 */
.form-group input {
  width: 100%;
  padding: 0.875rem 1rem;
  border: 2px solid var(--color-border);
  border-radius: var(--radius-md);
  font-size: 1rem;
  transition: all var(--transition-normal);
  background-color: var(--color-background-light);
}

.form-group input:focus {
  outline: none;
  border-color: var(--color-primary);
  background-color: white;
  box-shadow: 0 0 0 4px rgba(66, 185, 131, 0.1);
}

/* 注册按钮 */
.register-button {
  width: 100%;
  padding: 0.875rem;
  background: linear-gradient(135deg, var(--color-primary-light), var(--color-primary));
  color: white;
  border: none;
  border-radius: var(--radius-md);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition-normal);
  position: relative;
  overflow: hidden;
}

.register-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(66, 185, 131, 0.3);
}

.register-button:active {
  transform: translateY(0);
}

.register-button::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transition: all 0.5s;
}

.register-button:hover::before {
  left: 100%;
}

/* 登录链接 */
.login-link {
  text-align: center;
  margin-top: var(--spacing-lg);
  color: var(--color-text-secondary);
  font-size: 0.95rem;
}

.login-link a {
  color: var(--color-primary);
  text-decoration: none;
  font-weight: 500;
  transition: color var(--transition-normal);
}

.login-link a:hover {
  color: var(--color-primary-dark);
  text-decoration: underline;
}

/* 错误信息 */
.error-message {
  margin-top: var(--spacing-md);
  padding: var(--spacing-md);
  background-color: #ffebee;
  color: #c62828;
  border-radius: var(--radius-md);
  text-align: center;
  font-size: 0.9rem;
  animation: shake 0.5s;
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  10%, 30%, 50%, 70%, 90% { transform: translateX(-5px); }
  20%, 40%, 60%, 80% { transform: translateX(5px); }
}

/* 成功信息 */
.success-message {
  margin-top: var(--spacing-md);
  padding: var(--spacing-md);
  background-color: #e8f5e9;
  color: #2e7d32;
  border-radius: var(--radius-md);
  text-align: center;
  font-size: 0.9rem;
  animation: slideIn 0.5s ease-out;
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateY(-10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .register-container {
    padding: var(--spacing-md);
  }
  
  .register-card {
    padding: var(--spacing-lg);
  }
  
  .register-card h2 {
    font-size: 1.5rem;
  }
}
</style>